import 'dart:collection';
import 'dart:convert' as convert;
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_web_study/api/dio_utils.dart';
import 'package:flutter_web_study/utils/toast_utils.dart';
import 'package:logger/logger.dart';

import '../api/service_api.dart';
import '../bean/menu_item.dart';
import '../router/application.dart';
import '../router/routes.dart';

class CommodityManagementViewModel extends ChangeNotifier {
  int page1 = 1;
  int page2 = 2;

  MenuItemBean menuItemBean =
      MenuItemBean(value: 0, label: "全部", checked: false);

  //品牌名称
  final TextEditingController brandNameEidtText = TextEditingController();

  //系列名称
  final TextEditingController seriesNameEidtText = TextEditingController();

  //色号名称
  final TextEditingController colorNameEidtText = TextEditingController();

  //所有商品
  List listShopAll = [];
  List listShopAllSelect = [];
  BuildContext me;

  //总页数
  var sumNumber = 0;

  //商品分类

  CommodityManagementViewModel(this.me);

  initView() {
    // 请求接口
    DioUtils.getInstance()?.post(ServiceApi.QUERY_MAKEUP_GOODS, {
      "brand": brandNameEidtText.text.toString(),
      "series": seriesNameEidtText.text.toString(),
      "color": colorNameEidtText.text.toString(),
      "kind": menuItemBean.value,
      "page": page1,
      "pageSize": 20,
    }, (successCallBack) {
      listShopAll = successCallBack["data"]["list"];
      sumNumber = successCallBack["data"]["totalPage"];
      notifyListeners();
    }, (errorCallBack) {});
  }

  initData() {}

  //多选
  multipleChoice(value) {
    if (listShopAllSelect.contains(value)) {
      listShopAllSelect.remove(value);
    } else {
      listShopAllSelect.add(value);
    }
    notifyListeners();
  }

  //新建商品
  newShopJump() {
    Application().navigatePushToPage(me, Routes.newShopPage, {}).then((value) {
      if (value != null) {
        initView();
      }
    });
  }

  //编辑商品
  editShopJump(value) {
    Application().navigatePushToPage(me, Routes.newShopPage,
        {"jsonBean": convert.jsonEncode(value)}).then((value1) {
      Logger().d(value1);
      if (value1 != null) {
        for (int i = 0; i < listShopAll.length; i++) {
          if (value1["goodsId"] == listShopAll[i]["goodsId"]) {
            listShopAll[i] = value1;
            notifyListeners();
          }
        }
      }
    });
  }

  //翻页
  addPage(bool value) {
    print("=======$value");
    if (value == true) {
      // +
      if (page1 == sumNumber) {
        ToastUtils.showToast(msg: "已经是最后一页了");
        return;
      }
      page1++;
      page2++;
    } else {
      // -
      if (page1 == 1) {
        ToastUtils.showToast(msg: "已经是第一页了");
        return;
      }
      page1--;
      page2--;
    }
    notifyListeners();
    initView();
  }

  //删除商品
  deleteShop(value) {
    DioUtils.getInstance()
        ?.post(ServiceApi.REMOVE_MAKEUP_GOODS, {"goodsId": value["goodsId"]},
            (successCallBack) {
      ToastUtils.showToast(msg: "${successCallBack["message"]}");
      listShopAll.remove(value);
      listShopAllSelect.remove(value);
      notifyListeners();
    }, (errorCallBack) {
      initView();
      ToastUtils.showToast(msg: "${errorCallBack["message"]}");
    });
  }

  //添加商品评论
  productReview(value) {
    Application().navigatePushToPage(me, Routes.productReviewPage, {
      "tagPage": 0,
      "jsonBean": convert.jsonEncode(value),
    }).then((value1) {});
  }

  copyShop(value) {
    value["goodsId"] = "";
    DioUtils.getInstance()
        ?.post(ServiceApi.addMakeupGoods, {"json": jsonEncode(value)},
            (successCallBack) {
      ToastUtils.showToast(msg: "复制成功");
      initView();
    }, (errorCallBack) {});
  }

  getMenuItemBean(String value) {
    brandTypeList.forEach((element) {
      if (value == element.label) {
        menuItemBean = element;
      }
    });
    notifyListeners();
  }

  queryDate() {
    initView();
  }
}
